package com.wind.trademark.user.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wind.trademark.base.vo.NodeVo;
import com.wind.trademark.common.dao.BaseDao;
import com.wind.trademark.common.vo.PageBean;
import com.wind.trademark.common.vo.PageParam;
import com.wind.trademark.user.dao.UserDao;
import com.wind.trademark.user.model.AccRole;
import com.wind.trademark.user.model.AccUser;
import com.wind.trademark.user.model.AccUserRole;
import com.wind.trademark.user.service.UserService;


@Service
public class UserServiceImpl implements UserService {
	
	private final static String DEFAULT_PASSWORD ="ItSPazzWorD@TeP";
	@Autowired
	private UserDao userDao;
	@Autowired
	private BaseDao baseDao;
	
	
	@Override
	public PageBean queryUser(PageParam pageParam, Map<String, Object> params) {
		return userDao.queryUser(pageParam,params);
	}

	@Override
	public List<AccUser> getAllLawyer() {
		return baseDao.findAll(AccUser.class);
	}

	@Override
	public AccUser saveOrUpdateUser(AccUser user) {
		user.setCreateBy(1L);
		user.setCreateTime(new Date());
		user.setLastUpdateBy(1L);
		user.setLastUpdateTime(new Date());
		user.setAccount(user.getAccount().toLowerCase());
		user.setEmail(user.getEmail().toLowerCase());
		//md5
		if(DEFAULT_PASSWORD.equals(user.getPassword())){
			user.setPassword(this.baseDao.get(AccUser.class, user.getId()).getPassword());
			this.baseDao.merge(user);
		}else{
			user.setPassword(user.getPassword());
			this.baseDao.saveOrUpdate(user);
		}
		
		 return user;
	}

	@Override
	public AccUser getUser(Long id) {
		AccUser accUser=this.baseDao.get(AccUser.class, id);
		accUser.setPassword(DEFAULT_PASSWORD);
		return accUser;
	}

	@Override
	public List<NodeVo> loadRolesByUserId(Long userId) {
		List<AccUserRole> accUserRoleList=this.baseDao.findByProperty(AccUserRole.class, "userId", userId);
		List<AccRole> accRoleList=this.baseDao.findAll(AccRole.class);
		List<NodeVo> roleVoList=new ArrayList<NodeVo>();

		for(AccRole accRole:accRoleList){
			NodeVo roleVo=this.convert2NodeVo(accRole);
			this.checkPermission(roleVo,accUserRoleList);
			roleVoList.add(roleVo);
		}
		
		return roleVoList;
	}

	private void checkPermission(NodeVo roleVo,
			List<AccUserRole> accUserRoleList) {
		for(AccUserRole accUserRole:accUserRoleList){
			if(roleVo.getId().equals(accUserRole.getRoleId().toString())){
				roleVo.setChecked(true);
				break;
			}
		}
	}

	private NodeVo convert2NodeVo(AccRole accRole) {
		NodeVo nodeVo=new NodeVo();
		nodeVo.setId(accRole.getId().toString());
		nodeVo.setLeaf(true);
		nodeVo.setText(accRole.getName());
		nodeVo.setDisabled("0".equals(accRole.getEnable())?true:false);
		return nodeVo;
	}

	@Override
	public void saveOrUpdateUserRole(Long userId, List<Long> roleList) {

		List<AccUserRole> accUserRoleList=this.baseDao.findByProperty(AccUserRole.class, "userId", userId);
		for(AccUserRole accUserRole:accUserRoleList){
			this.baseDao.delete(accUserRole);
		}
		for(Long roleId:roleList){
			AccUserRole accUserRole=new AccUserRole();
			accUserRole.setRoleId(roleId);
			accUserRole.setUserId(userId);
			this.baseDao.saveOrUpdate(accUserRole);
		}
	
	}
	 
	@Override
	public boolean checkAccountUsed(Map<String,Object> params) {
		List<AccUser> list=this.userDao.checkAccountUsed(params);
		return !list.isEmpty();

	}


	
	
	
} 